package com.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.system.annotation.SystemLogAnnotation;
import com.system.common.BaseController;
import com.system.common.ResponseCode;
import com.system.constants.SysLogType;
import com.system.entity.dto.SearchDTO;
import com.system.entity.dto.UserRoleEditDTO;
import com.system.entity.manage.SysLog;
import com.system.entity.manage.SysPage;
import com.system.entity.manage.SysRole;
import com.system.entity.manage.SysUser;
import com.system.entity.vo.RoleDataVO;
import com.system.entity.vo.RspResultVO;
import com.system.service.SystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统管理相关接口
 *
 * @author 佛冷
 * @date 2019/10/8
 */
@Api(tags = "系统管理相关接口")
@RestController
@RequestMapping("/system")
public class SystemController extends BaseController {

    @Autowired
    private SystemService systemService;

    @ApiOperation("分页获取用户列表")
    @GetMapping("/users")
    @SystemLogAnnotation(operationName = "分页获取用户列表", businessType = SysLogType.SELECT_LIST_PAGE)
    public RspResultVO<IPage<SysUser>> listUser(SearchDTO dto) {
        return returnSuccess(systemService.listUser(dto));
    }


    @ApiOperation("根据用户id获取用户信息")
    @GetMapping("/users/{userId}")
    @SystemLogAnnotation(operationName = "根据用户id获取用户信息", businessType = SysLogType.SELECT_ONE)
    public RspResultVO<SysUser> getUser(@ApiParam("账号id") @PathVariable Integer userId) {
        return returnSuccess(systemService.getUser(userId));
    }

    @ApiOperation("编辑用户信息")
    @PostMapping("/users")
    @SystemLogAnnotation(operationName = "编辑用户信息", businessType = SysLogType.EDIT)
    public RspResultVO editUser(@RequestBody SysUser dto) {
        int count = systemService.editUser(dto);
        if (count > 0) {
            return returnSuccess();
        }
        if (count == -1) {
            return returnResult(ResponseCode.EDIT_ERROR.getCode(), "该账户名称已存在");
        }
        return returnResult(ResponseCode.EDIT_ERROR);
    }

    @ApiOperation("编辑用户权限")
    @PostMapping("/users/roles")
    @SystemLogAnnotation(operationName = "编辑用户权限", businessType = SysLogType.EDIT)
    public RspResultVO editUserRoles(@RequestBody SysUser dto) {

        if (dto.getUserId() == null) {
            return returnResult(ResponseCode.PARAMS_ERROE.getCode(), "用户ID不能为空");
        }
        int count = systemService.editUserRoles(dto);
        return returnSuccess();
    }

    @ApiOperation("编辑用户状态")
    @PostMapping("/users/status")
    @SystemLogAnnotation(operationName = "编辑用户状态", businessType = SysLogType.EDIT)
    public RspResultVO editUserStatus(@RequestBody SysUser dto) {
        int count = systemService.editUserStatus(dto);
        if (count > 0) {
            return returnSuccess();
        }
        return returnResult(ResponseCode.EDIT_ERROR);
    }

    @ApiOperation("删除用户信息")
    @DeleteMapping("/users/{userId}")
    @SystemLogAnnotation(operationName = "删除用户信息", businessType = SysLogType.DELETE)
    public RspResultVO deleteUser(@ApiParam("账号id") @PathVariable Integer userId) {
        int count = systemService.delUser(userId);
        if (count > 0) {
            return returnSuccess();
        }
        return returnResult(ResponseCode.EDIT_ERROR);
    }

    @ApiOperation("分页获取页面列表")
    @GetMapping("/pages")
    @SystemLogAnnotation(operationName = "分页获取页面列表", businessType = SysLogType.SELECT_LIST_PAGE)
    public RspResultVO<IPage<SysPage>> listPage(SearchDTO dto) {
        return returnSuccess(systemService.listPage(dto));
    }

    @ApiOperation("获取目录页面 pageId 除外")
    @GetMapping("/pages/list")
    @SystemLogAnnotation(operationName = "获取目录页面 pageId 除外", businessType = SysLogType.SELECT_LIST)
    public RspResultVO<List<SysPage>> listPage(@RequestParam Integer pageId) {
        return returnSuccess(systemService.listPage(pageId));
    }

    @ApiOperation("编辑页面信息")
    @PostMapping("/pages")
    @SystemLogAnnotation(operationName = "编辑页面信息", businessType = SysLogType.EDIT)
    public RspResultVO editPage(@RequestBody SysPage dto) {
        int count = systemService.editPage(dto);
        if (count > 0) {
            return returnSuccess();
        }
        if (count == -1) {
            return returnResult(ResponseCode.EDIT_ERROR.getCode(), "该菜单已经关联子菜单暂无法修改类型");
        }
        return returnResult(ResponseCode.EDIT_ERROR);
    }

    @ApiOperation("编辑页面状态信息")
    @PostMapping("/pages/status")
    @SystemLogAnnotation(operationName = "编辑页面状态信息", businessType = SysLogType.EDIT)
    public RspResultVO editPageStatus(@RequestBody SysPage dto) {
        int count = systemService.editPageStatus(dto);
        if (count > 0) {
            return returnSuccess();
        }
        return returnResult(ResponseCode.EDIT_ERROR);
    }

    @ApiOperation("删除页面信息 （by 佛冷）")
    @DeleteMapping("/pages/{pageId}")
    @SystemLogAnnotation(operationName = "删除页面信息", businessType = SysLogType.DELETE)
    public RspResultVO delPage(@ApiParam("页面id") @PathVariable Integer pageId) {
        int count = systemService.delPage(pageId);
        if (count > 0) {
            return returnSuccess();
        }
        if (count == -1) {
            return returnResult(ResponseCode.EDIT_ERROR.getCode(), "该菜单已经关联子菜单暂无法删除");
        }
        return returnResult(ResponseCode.EDIT_ERROR);
    }

    @ApiOperation("获取角色列表")
    @GetMapping("/roles")
    @SystemLogAnnotation(operationName = "分页获取角色列表", businessType = SysLogType.SELECT_LIST_PAGE)
    public RspResultVO<IPage<SysRole>> listRole(SearchDTO dto) {
        return returnSuccess(systemService.listRole(dto));
    }

    @ApiOperation("获取角色列表")
    @GetMapping("/roles/list")
    @SystemLogAnnotation(operationName = "获取角色列表", businessType = SysLogType.SELECT_LIST)
    public RspResultVO<List<SysRole>> listRole() {
        return returnSuccess(systemService.listRole());
    }

    @ApiOperation("获取角色权限信息")
    @GetMapping("/roles/{roleId}")
    @SystemLogAnnotation(operationName = "获取角色权限信息", businessType = SysLogType.SELECT_LIST)
    public RspResultVO<RoleDataVO> listRolePage(@ApiParam("角色id") @PathVariable Integer roleId) throws CloneNotSupportedException {
        return returnSuccess(systemService.listRolePage(roleId));
    }

    @ApiOperation("编辑角色信息")
    @PostMapping("/roles")
    @SystemLogAnnotation(operationName = "编辑角色信息", businessType = SysLogType.EDIT)
    public RspResultVO editRole(@RequestBody SysRole dto) {
        int count = systemService.editRole(dto);
        if (count > 0) {
            return returnSuccess();
        }
        return returnResult(ResponseCode.EDIT_ERROR);
    }

    /**
     * 编辑用户权限
     *
     * @author 佛冷
     * @date 2019/10/8
     */
    @ApiOperation("编辑用户权限")
    @PostMapping("/rolePages")
    @SystemLogAnnotation(operationName = "编辑用户权限", businessType = SysLogType.EDIT)
    public RspResultVO editRolePage(@RequestBody UserRoleEditDTO dto) {
        int count = systemService.editRolePage(dto);
        if (count > 0) {
            return returnSuccess();
        }
        return returnResult(ResponseCode.EDIT_ERROR);
    }

    @ApiOperation("删除角色信息")
    @DeleteMapping("/roles/{roleId}")
    @SystemLogAnnotation(operationName = "删除角色信息", businessType = SysLogType.DELETE)
    public RspResultVO delRole(@ApiParam("roleId") @PathVariable Integer roleId) {
        int count = systemService.delRole(roleId);
        if (count > 0) {
            return returnSuccess();
        }
        return returnResult(ResponseCode.EDIT_ERROR);
    }

    @ApiOperation("获取操作日志")
    @GetMapping("/logs")
    public RspResultVO<IPage<SysLog>> pageSysLog(SearchDTO dto) {
        return returnSuccess(systemService.pageSysRole(dto));
    }

}
